package cn.tedu.contorller;

import cn.tedu.utils.ThUtils;
import org.thymeleaf.context.Context;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;

@WebServlet(name = "ShowLoginServlet",urlPatterns = {"/ShowLoginServlet"})
public class ShowLoginServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Context context = new Context();
        //获取Cookie中的用户名和密码
        Cookie[] cookies = request.getCookies();

        if (cookies!=null){
            for (Cookie cookie:cookies) {
                if (cookie.getName().equals("username")){
                    //获取cookie中对应key值的value值
                    String username = cookie.getValue();
                    //将获取到的可以存在中文的value值进行url解码
                    username = URLDecoder.decode(username,"UTF-8");
                    context.setVariable("name",username);
                }
                if (cookie.getName().equals("password")){
                    String password = cookie.getValue();
                    context.setVariable("pwd",password);
                }
            }
        }

        ThUtils.print("login.html",context,response);
    }
}
